package com.mobile.service.impl.system;

import com.mobile.service.dao.DaoSupport;
import com.mobile.service.inter.system.OrganizationService;
import com.mobile.util.Const;
import com.mobile.util.PageData;
import com.mobile.util.StringUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * 组织管理模块Service实现类
 *
 * @author Administrator
 */
@Service("organizationService")
@Transactional
public class OrganizationServiceImpl implements OrganizationService {
    @Resource(name = "daoSupport")
    private DaoSupport dao;

    /**
     * 单位列表查询（分页）
     * @param pageData
     * @return
     * @throws Exception
     */
    @Override
    public List<PageData> companyList(PageData pageData) throws Exception {
        return (List<PageData>) dao.findForList("MOrganizationMapper.companylist", pageData);
    }

    /**
     * 部门列表查询
     * @param pd
     * @return
     * @throws Exception
     */
    @Override
    public List<PageData> getOrganizationTreeList(PageData pd) throws Exception {
        return (List<PageData>) dao.findForList("MOrganizationMapper.getOrganizationTreeList", pd);
    }
    /**
     * 部门列表查询
     * @param pd
     * @return
     * @throws Exception
     */

    /**
     * 根据条件获取单位信息
     * @param pageData
     * @return
     */
    @Override
    public List<PageData> getOrganizationBySerch(PageData pageData) throws Exception {
        return (List<PageData>) dao.findForList("MOrganizationMapper.getOrganizationBySerch", pageData);
    }

    /**
     * 根据条件获取部门信息
     * @param pageData
     * @return
     */
    @Override
    public List<PageData> getCompanyBySerch(PageData pageData) throws Exception {
        return (List<PageData>) dao.findForList("MOrganizationMapper.getCompanyBySerch", pageData);
    }

    @Override
    public List<PageData> getCompanyByfactoryCode(PageData pageData) throws Exception {
        return (List<PageData>) dao.findForList("MOrganizationMapper.getCompanyByfactoryCode", pageData);
    }

    @Override
    public List<PageData> getDepartmentByNameAndcompanyId(PageData pageData) throws Exception {
        return (List<PageData>) dao.findForList("MOrganizationMapper.getDepartmentByNameAndcompanyId", pageData);
    }

    /**
     * 新增单位
     * @param pageData
     * @return
     */
    @Override
    public int insertCompany(PageData pageData) throws Exception {
        return (int) dao.save("MOrganizationMapper.insertCompany",pageData);
    }

    /**
     * 新增部门
     * @param pageData
     * @return
     */
    @Override
    public int insertOrganization(PageData pageData) throws Exception {
        //同单位下 相同名称 返回-1  由控制层判断
        if (!getOrganizationBySerch(pageData).isEmpty()) {
            return -1;
        }//当erpId不为空的时候判断是否为允许新增的单位名称
        if (!StringUtil.isEmpty(pageData.get("erpDepId"))&& !Arrays.asList(Const.ALLOW_ERP_DEP_ID).contains(pageData.getString("organizationName"))) {
            return -2;
        }
        return (Integer) dao.save("MOrganizationMapper.insertOrganization",pageData);
    }

    /**
     * 修改单位
     * @param pageData
     * @return
     */
    @Override
    public int updateCompany(PageData pageData) throws Exception {
        return (int) dao.update("MOrganizationMapper.updateCompany",pageData);
    }

    /**
     * App修改单位
     * @param pageData
     * @return
     */
    @Override
    public int appUpdateCompany(PageData pageData) throws Exception {
        return (int) dao.update("MOrganizationMapper.appUpdateCompany",pageData);
    }

    /**
     * 修改部门
     * @param pageData
     * @return
     */
    @Override
    public int updateDepartment(PageData pageData) throws Exception {
        //同单位下 不同编号 相同名称 返回-1  由控制层判断
        PageData org = new PageData();
        org.put("organizationName", pageData.get("organizationName"));
        org.put("organizationIdCheck", pageData.get("organizationId"));
        List<PageData> orgs = (List<PageData>) dao.findForList("MOrganizationMapper.getOrganizationBySerch", org);
        //根据单位id和部门名称查询,无同名或同名为本部门可校验通过
        if (!orgs.isEmpty()) {
            return -1;
        }
        //当erpId不为空的时候判断是否为允许新增的单位名称
        if (!StringUtil.isEmpty(pageData.get("erpDepId"))&& !Arrays.asList(Const.ALLOW_ERP_DEP_ID).contains(pageData.getString("organizationName"))) {
            return -2;
        }
        return (int) dao.update("MOrganizationMapper.updateOrganization",pageData);

    }

    /**
     * 删除单位
     * @param array
     * @return
     */
    @Override
    public int deleteCompanyByBatch(String[] array) throws Exception {
        return (int) dao.delete("MOrganizationMapper.deleteCompanyByBatch",array);
    }

    /**
     * 删除部门
     * @param array
     * @return
     */
    @Override
    public int deleteOrganizationByBatch(String[] array) throws Exception {
        return (int) dao.delete("MOrganizationMapper.deleteOrganizationByBatch",array);
    }
    /**
     * 根据条件获取单位信息
     * @param pageData
     * @return
     */
    @Override
    public List<PageData> getAppDepartmentBySerch(PageData pageData) throws Exception {
        return (List<PageData>) dao.findForList("MOrganizationMapper.getAppDepartmentBySerch", pageData);
    }

    /**
     * 根据条件获取部门信息
     * @param pageData
     * @return
     */
    @Override
    public List<PageData> getAppCompanyBySerch(PageData pageData) throws Exception {
        return (List<PageData>) dao.findForList("MOrganizationMapper.getAppCompanyBySerch", pageData);
    }

    /**
     * 用于查询当前单位与其下属单位编号
     * @param companyId
     * @return
     * @throws Exception
     */
    @Override
    public String getCompanyRecord(Integer companyId)throws Exception {
        return (String) dao.findForObject("MOrganizationMapper.getCompanyRecord", companyId);
    }
    /**
     * 根据id获取单位
     * @param companyId
     * @return
     * @throws Exception
     */
    @Override
    public PageData getCompanyById(Integer companyId)throws Exception {
        return (PageData) dao.findForObject("MOrganizationMapper.getCompanyById", companyId);
    }

    /**
     * 获取关联单位穿梭框数据
     * @param pd
     * @return
     * @throws Exception
     */
    @Override
    public List<PageData> getCompanyListNotInmySelf(PageData pd) throws Exception {
        return (List<PageData>) dao.findForList("MOrganizationMapper.getCompanyListNotInmySelf", pd);
    }

    /**
     * 获取组织所有上级id
     */
    @Override
    public List<String> getAllParentId(String organizationId, List<String> list) throws Exception {
        PageData pd = new PageData();
        pd.put("organizationId", organizationId);
        List<PageData> orgs = this.getOrganizationBySerch(pd);
        if (orgs.isEmpty()) {
            return list;
        }
        list.add(organizationId);
        PageData org = orgs.get(0);
        getAllParentId(org.get("parentId").toString(), list);
        return list;
    }
    /**
     * 获取组织所有下级id
     */
    @Override
    public List<String> getAllLowId(String organizationId, List<String> list) throws Exception {
        PageData pd = new PageData();
        pd.put("parentId", organizationId);
        List<PageData> orgs = this.getOrganizationBySerch(pd);
        if (orgs.isEmpty()) {
            return list;
        }
        for (PageData pageData : orgs) {
            String orgId = pageData.get("organizationId").toString();
            list.add(orgId);
            getAllLowId(orgId, list);
        }
        return list;
    }
}
